Skip to main content

6.1 Bindings

Bindings are essentially global variables/parameters that can be referred to across datasets. This allows you to generalize/automate a script. Bindings are typically defined at the top of a script using the let command and can point to a numeric value, a string (text), a symbol, a binding, or combinations of these.

A symbol is a specific object that you will later refer to, usually a variable. It does not necessarily have to be an existing variable but can be inserted into an expression where the variable is to be generated. Symbols should not be specified with quotes like strings.

A typical use case for bindings is when referring to years or dates in connection with the import of variables, preparation of variables, running statistics, etc. You can then create a binding to a particular year where it is relevant, and if you find out that you want to run for another year, you just adjust the value of the binding instead of all places in the script where the year is located. In the script, use the $ sign followed by the name of the binding where you want to point to the binding.

Examples of defining bindings:

 let varnavn = "income"

let yyyy = 2021
let yy = $yyyy - 2000

let approx_pi = 22/7
 

Use help let for the full syntax description for bindings.

You can also use functions that only operate on bindings, also called procedures, in this way:

 let pi = pi()
let en = cos(2 * pi())
let dt3 = date_fmt($yyyy, 10, 20)
 

Use the help-procedure command to read about what other procedures are available.

It is possible to assemble and combine several bindings into one expression by using the ++ signs. This corresponds to concatenating values/text/symbols into one combined expression. Note that numbers combined with text become text. Example:

 let date = $yyyy ++ "-01-01"
 

Example of using bindings:

 let date = date_fmt(2021,08,01) 
let siv = sivstate

import f/SIVSTANDFDT_SIVSTAND $date as $siv
 

In the example above, the date_fmt() procedure is used. This is a very useful procedure that generates standard target date dates for import commands (in the form YYYY-MM-DD), and can be used to make the script even more automated and streamlined. Example:

 let dt1 = date_fmt(2021)
let dt2 = date_fmt(2021, 10)
let dt3 = date_fmt(2021, 10, 20)
 

The commands above generate the following date values:

  • 2021-01-01

  • 2021-10-01

  • 2021-10-20

Examples of how to generalize dates in an import expression:

 let date = $yyyy ++ "-01-01"
import f/SIVSTANDFDT_SIVSTAND $date as sivstate
 

equals

 import f/SIVSTANDFDT_SIVSTAND date_fmt($yyyy) as sivstate
 

It may be wise to think twice about using inline bindings (i.e. use of ++), as the code can become more difficult to read with excessive use. Any error messages can also become more difficult to deal with. A rule of thumb is that you define a fully concatenated binding through let if it is used in several places.

NB! Note that using inline bindings (++) is not compatible with the collapse() command. Therefore, you should use bindings through the use of let in conjunction with collapse() operations.

Example:

Note that ++ has lower precedence than the mathematical operators + and -. This means that the expression

generate married = 1 if siv_ ++ $year + 2 == 1

is the same as

generate married = 1 if siv_ ++ ($year + 2) == 1